package com.cxg.fairy.ec.main.personal.favor;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.SwipeItemLayout;
import com.diabin.latte.ec.R2;
import com.diabin.latte.ec.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/8.
 */

public class FavorDelegate extends LatteDelegate implements ISuccess {

    private static final String ARG_VIEW_TITLE = "ARG_VIEW_TITLE";

    private FavorAdapter favorAdapter = null;

    @BindView(R2.id.rv_favor_list)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.titleText)
    TextView titleView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_favor_list;
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("getFavoriteFullByConsumerId")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSuccess(String response) {
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);

        final List<MultipleItemEntity> data = new FavorDataConverter().setJsonData(response).convert();
        favorAdapter = new FavorAdapter(data);
        favorAdapter.setFavorDelegate(this);
        mRecyclerView.setAdapter(favorAdapter);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this.getContext(),DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.my_decoration_single));
        mRecyclerView.addItemDecoration(dividerItemDecoration);

        mRecyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(getContext()));

    }

    public void updateRecyclerView(String response) {
        favorAdapter.getData().clear();
        FavorDataConverter favorDataConverter = new FavorDataConverter();
        ArrayList<MultipleItemEntity> datas = favorDataConverter.setJsonData(response).convert();
        favorAdapter.getData().addAll(datas);
        favorAdapter.notifyDataSetChanged();
    }


}
